/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * \file
 * \brief Gdb wrapper for Gimbo components.
 * \author Antti Maula <antti.maula@tkk.fi>
 *
 * Usage: Create a file with name 'GdbWrapper.conf' in the same
 * directory as this wrapper. File must be formatted like:
 * <working directory>\n
 * <application>\n
 * <gdb parameters>\n
 * [whatever you like :) ...]
 */
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <assert.h>

int main(int argc, char *argv[])
{
  int result = 1;
  int r;
  std::string workdir;
  std::string gdbparam;
  std::string app;
  std::string param;
  std::string final_command;

  const std::string confname = std::string(argv[0]) + std::string(".conf");
  FILE *vgwrapc = fopen(confname.c_str(), "rb");
  if (vgwrapc) {
    char tmp[1024];

    // Read workdir
    if ( fgets(tmp, sizeof(tmp), vgwrapc) == NULL ) {
      perror("Failed to read WorkingDirectory");
      exit(1);

    } else {
      workdir = tmp;
      workdir.resize(workdir.size()-1);
      printf("Workdir set to: '%s'\n", workdir.c_str());
    }
    
    // Read app
    if ( fgets(tmp, sizeof(tmp), vgwrapc) == NULL ) {
      perror("Failed to read Application");
      exit(1);
      
    } else {
      app = tmp;
      app.resize(app.size()-1);
      printf("Application set to: '%s'\n", app.c_str());
    }

  } else {
    printf("'%s' not found. Please create it.\n", confname.c_str());
    exit(1);
  }

  // Store param (Always add space)
  for (int i=1; i < argc; ++i) {
    param += std::string(" ") + std::string(argv[i]);
  }

  //// Execute.
  // Change wd.
  r = chdir(workdir.c_str());
  if (r == 0) {
    // Construct GDB batch file to execute the application.
    std::string batchfile = "gdbWrapper.gdbbatch";
    std::string gdbbatch;
    FILE *gdbbf = fopen(batchfile.c_str() ,"wb");
    perror("asd");
    assert(gdbbf != NULL);
    gdbbatch += "run" + param + "\n";
    fprintf(gdbbf, "%s", gdbbatch.c_str());
    fclose(gdbbf);


    // Construct final command to run
    // Use this to get I/O in same window.
    // final_command = "gdb " + gdbparam + " -x " + batchfile + " " + app;
    // Use this to get I/O on separate konsole.
    char wd[1024];
    char *f = getcwd(wd, sizeof(wd));
    assert(f != NULL);

    final_command = "konsole --nofork --workdir  " + std::string(wd) + " -e gdb " + gdbparam + " -x " + batchfile + " " + app;

    printf("Gdb Wrapper :: Executing final command: '%s' in current directory: '%s'\n",
           final_command.c_str(), wd);
    result = system(final_command.c_str());

    // Close tempfile
//    unlink(batchfile.c_str());
    
  }


  return result;
}

/**
 * 
 * $Id: gdbWrapper.cpp,v 1.6 2009-11-17 11:33:04 amaula Exp $
 *
 ******************************************************************************
 *
 * $Log: not supported by cvs2svn $
 * Revision 1.5  2009-11-17 11:29:00  amaula
 * Added support for differrent konsole version which now preserves working dir in cmd line
 *
 * Revision 1.4  2009-05-05 10:31:27  amaula
 * Added note about konsole and example (also set as default :)
 *
 * Revision 1.3  2009-05-05 10:12:52  amaula
 * Fixed some issues.
 *
 * Revision 1.2  2009-05-05 10:02:39  amaula
 * cleanup
 *
 * Revision 1.1  2009-05-05 10:02:19  amaula
 * gdbWrapper added.
 *
 */
